//
//  FadeMovieClip
//
//  Created by Wonil Suh on 2009-07-15.
//  Copyright (c) 2009 secret toilet. All rights reserved.
//  Contact: wosuh@secretToilet.com
//  
//  DESCRIPTION
//  

package com.wonilsuh.display {
	
	// import
	import flash.display.MovieClip;
	
	import com.wonilsuh.display.IFader;
	import com.wonilsuh.display.Fader;
	
	import flash.events.Event;
	
	import fl.transitions.easing.*;
	
	public class FadeMovieClip extends MovieClip implements IFader {

		// properties
		private var FadeMovieClip_fader:Fader;
		
		// timeline elements
		
		// constants
		public static const FADEMOVIECLIP_FADE_COMPLETE:String = "FADEMOVIECLIP_FADE_COMPLETE";
		
		//----------------------------------------
		// constructor
		public function FadeMovieClip() {
			//trace("FadeMovieClip has been constructed.");
			FadeMovieClip_fader = new Fader(this);
			
			setEasingFunction(Strong.easeOut);
		}

		//----------------------------------------
		// init methods
		
		//----------------------------------------
		// methods
		
		//----------------------------------------
		// getters and setters
		
		//----------------------------------------
		// interface implementations
		
		//----------------------------------------
		// interface implementations: com.wonilsuh.display.IFader
		
		public function setEasingFunction(pFn:Function):void {
			//trace(".setEasingFunction()...");
			FadeMovieClip_fader.setEasingFunction(pFn);
		}
		
		public function fade(_from:Number=0,_to:Number=1,_duration:Number=1, pHideAfterFO:Boolean=true):void {
			//trace("FadeSprite.fade()...");
			FadeMovieClip_fader.addEventListener(Fader.FADER_FADE_COMPLETE, fadeMovieClip_onFadeComplete);
			FadeMovieClip_fader.fade(_from, _to, _duration, pHideAfterFO);
		}
		
		private function fadeMovieClip_onFadeComplete(e:Event):void {
			//trace("FadeSprite.fadeSprite_onFadeComplete()...");
			FadeMovieClip_fader.removeEventListener(Fader.FADER_FADE_COMPLETE, fadeMovieClip_onFadeComplete);
			dispatchEvent(new Event(FADEMOVIECLIP_FADE_COMPLETE));
		}
				
		public function fadeTo(_to:Number=1,_duration:Number=1):void {
			//trace("FadeSprite.fadeTo()...");
			FadeMovieClip_fader.addEventListener(Fader.FADER_FADE_COMPLETE, fadeMovieClip_onFadeComplete);
			FadeMovieClip_fader.fadeTo(_to, _duration);
		}
		
		public function fadeIn(_duration:Number=1):void {
			//trace("FadeSprite.fadeIn()...");
			FadeMovieClip_fader.addEventListener(Fader.FADER_FADE_COMPLETE, fadeMovieClip_onFadeComplete);
			FadeMovieClip_fader.fadeIn(_duration);
		}
		
		public function fadeOut(_duration:Number=1, pHideAfterFO:Boolean=true):void {
			//trace("FadeSprite.fadeOut()...");
			FadeMovieClip_fader.addEventListener(Fader.FADER_FADE_COMPLETE, fadeMovieClip_onFadeComplete);
			FadeMovieClip_fader.fadeOut(_duration, pHideAfterFO);
		}
		
	}
}